//
//  LiuHomeViewController.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/26.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit

class LiuHomeViewController: LiuBaseViewController {
    
    
    //viewmodel
    fileprivate lazy var homeViewModel:LiuHomeViewModel = LiuHomeViewModel()
    
    //上拉添加
    fileprivate lazy var pullUpView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    
    //下拉刷新
    fileprivate lazy var pulldownView:LiuRefreshControl = {
        let refresh = LiuRefreshControl()
        refresh.addTarget(self, action: #selector(pulledownData), for: .valueChanged)
        return refresh
    }()
    
    //刷新报告label
    fileprivate lazy var tipLabel:UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.white, text: "")
        label.textAlignment = .center
        label.backgroundColor = UIColor.orange
        label.isHidden = true
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
            self.visitorView?.setLoginView(imageName: nil, messageStr: nil)
        } else {
            setupTableViewUI()
            loadData()
        }
    }
    
    fileprivate func setupTableViewUI() {
        
        //设置背景颜色
//        tableView.backgroundColor = UIColor.groupTableViewBackground
        //注册cell
        tableView.register(LiuHomeTableViewCell.self, forCellReuseIdentifier: HomeCellID)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 250
        
        //隐藏分割线
        tableView.separatorStyle = .none
        
        //设置上拉添加
        tableView.tableFooterView = pullUpView
        
        //添加下拉刷新
        tableView.addSubview(pulldownView)
        
        //设置tipView
        if let nav = navigationController {
            
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)
        }
        
        
        
    }
    
    fileprivate func loadData() {
        
        homeViewModel.loadHomeData(isPullup: pullUpView.isAnimating) { (isSuccess) in
            if isSuccess {
                
                //只有在下拉刷新的时候才去弹出提示框,并且tip是隐藏状态
                if !self.pullUpView.isAnimating && self.tipLabel.isHidden {
                    self.tipLabelAnimation()
                }
                
                self.pullUpView.stopAnimating()
                self.tableView.reloadData()
                
                
                
                
            }
        }
        
    }
    
    //tip动画
    fileprivate func tipLabelAnimation() {
        
        UIView.animate(withDuration: 1, animations: { 
            //给label.text赋值
            self.tipLabel.text = self.homeViewModel.tipContent
            
            //显示label
            self.tipLabel.isHidden = false
            
            //改变transform
            self.tipLabel.transform = CGAffineTransform.init(translationX: 0, y: self.tipLabel.height)
        }) { (_) in
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: { 
                UIView.animate(withDuration: 1, animations: {
                    //改变transform
                    self.tipLabel.transform = CGAffineTransform.identity
                }, completion: { (_) in
                    //隐藏label
                    self.tipLabel.isHidden = true
                })
            })
        }
    }
    
    //下拉刷新
    @objc fileprivate func pulledownData() {
        loadData()
        pulldownView.endRefreshing()
    }
}


//数据源代理方法
extension LiuHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.homeStatuses.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:LiuHomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: HomeCellID, for: indexPath) as! LiuHomeTableViewCell
        
        let viewModel = homeViewModel.homeStatuses[indexPath.row]
        
        cell.viewModel = viewModel
        
        return cell
        
    }
    
    //显示最后一个cell之前开启滚动
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == homeViewModel.homeStatuses.count - 1 && !pullUpView.isAnimating {
            pullUpView.startAnimating()
            //开始请求数据
            loadData()
        }
    }
}









